package com.ostrich.note.ui.calendar;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.necer.calendar.BaseCalendar;
import com.necer.enumeration.DateChangeBehavior;
import com.necer.listener.OnCalendarChangedListener;
import com.ostrich.note.R;
import com.ostrich.note.base.BaseFragment;
import com.ostrich.note.base.BaseMvvmFragment;
import com.ostrich.note.databinding.FragmentCalendarBinding;
import com.ostrich.note.entity.BaseResponseEntity;
import com.ostrich.note.ui.calendar.viewmodel.CalendarViewModel;
import com.ostrich.note.ui.note.entity.NoteEntity;
import com.ostrich.note.utils.DialogUtils;

import org.joda.time.LocalDate;

import java.io.DataInput;
import java.util.ArrayList;
import java.util.List;


public class CalendarFragment extends BaseMvvmFragment<FragmentCalendarBinding, CalendarViewModel> {

    @Override
    public int getLayout() {
        return R.layout.fragment_calendar;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {

    }

    @Override
    public void initListener() {


    }


    @Override
    public void onResume() {
        super.onResume();
        binding.miui9Calendar.setOnCalendarChangedListener(new OnCalendarChangedListener() {
            @Override
            public void onCalendarChange(BaseCalendar baseCalendar, int year, int month, LocalDate localDate, DateChangeBehavior dateChangeBehavior) {
                binding.tvDate.setText(year+"年"+month+"月");

                viewModel.noteTimeDayList(year,month,localDate.getDayOfMonth()).observe(CalendarFragment.this, new Observer<BaseResponseEntity<List<NoteEntity>>>() {
                    @Override
                    public void onChanged(BaseResponseEntity<List<NoteEntity>> listBaseResponseEntity) {
                        if(listBaseResponseEntity.getCode() == 200 && listBaseResponseEntity.getData().size() >0){
                            Bundle bundle = new Bundle();
                            ArrayList<NoteEntity> data = (ArrayList<NoteEntity>) listBaseResponseEntity.getData();
                            bundle.putSerializable("list",data);
                            bundle.putString("date",year+"年"+month);
                            ToastUtils.showShort("有笔记");
                            startActivityBundle(CalendarNoteActivity.class,bundle);
                        }else{
                            ToastUtils.showShort("没笔记");
                            DialogUtils.showNoNoteDialog(getActivity(), new DialogUtils.DialogListener() {
                                @Override
                                public void onCancelClick() {

                                }

                                @Override
                                public void onConfirmClick() {

                                }

                                @Override
                                public void onConfirmClick(String data) {

                                }

                                @Override
                                public void onCheckChanged(boolean isChecked) {

                                }
                            });

                        }
                    }
                });
            }

        });
    }

    @Override
    public void createViewModel() {
        viewModel = new ViewModelProvider(this).get(CalendarViewModel.class);
    }
}